<?php
/***
 * 后台权限、角色变更日志model
 * Class Admin_operate_log_model
 */

class Admin_operate_log_model extends CT_Model
{

    public $_table = 'admin_operate_log';

    public $_table_admin = 'admin';

    public function __construct()
    {
        parent::__construct();
        $this->load->database();
    }

    public  function get_list($condition,$offset = 0, $limit = 20)
    {

        $this->db->select();
        $this->db->from($this->_table);
        $this->db->where('status',0);
        if(!empty($condition['action_name']))
        {
            $this->db->like('action_name',$condition['action_name']);

        }
        if(!empty($condition['new_content']))
        {
            $this->db->like('new_content',$condition['new_content']);

        }
        if(!empty($condition['old_content']))
        {
            $this->db->like('old_content',$condition['old_content']);

        }
        $this->db->limit($limit, $offset);
        $this->db->order_by('create_time desc');
        $query = $this->db->get();
        return $query->result_array();

    }

    public  function get_list_count($condition)
    {
        $this->db->select();
        $this->db->from($this->_table);
        $this->db->where('status',0);
        if(!empty($condition['action_name']))
        {
            $this->db->like('action_name',$condition['action_name']);

        }
        if(!empty($condition['new_content']))
        {
            $this->db->like('new_content',$condition['new_content']);

        }
        if(!empty($condition['old_content']))
        {
            $this->db->like('old_content',$condition['old_content']);

        }
        return $this->db->count_all_results();

    }


    /**
     * 获取临时表操作日志列表
     * @param $condition
     * @param int $offset
     * @param int $limit
     * @return mixed
     */
    public  function get_list_tmp($condition,$offset = 0, $limit = 20)
    {

        $sql="SELECT ao.* FROM `".$this->_table .'` AS ao LEFT JOIN `'.$this->_table_admin.'` AS a ON ao.uid=a.id';

        $sql.=" WHERE ao.status=0 and action_name='商品管理'";


        if(!empty($condition['admin_name']))
        {
            $sql.=' AND ( a.username LIKE  "%'.$condition['admin_name'].'%" OR a.nickname LIKE "%'.$condition['admin_name'].'%")';

        }


        if(!empty($condition['start_date']))
        {
            $sql.=" AND ao.create_time >='".$condition['start_date']."'";
        }

        if(!empty($condition['end_date']))
        {
            $sql.="  AND ao.create_time <='".$condition['end_date']."'";
        }

        $sql .= " ORDER BY ao.create_time DESC ";
        $sql .=" LIMIT " . $limit;
        $sql .= " OFFSET ". $offset;
        return $this->db->query($sql)->result_array();

    }

    public  function get_list_tmp_count($condition)
    {
        $sql="SELECT COUNT(ao.id) as total FROM `".$this->_table .'` AS ao LEFT JOIN `'.$this->_table_admin.'` AS a ON ao.uid=a.id';


        $sql.=" WHERE ao.status=0 and action_name='商品管理'";


        if(!empty($condition['admin_name']))
        {
            $sql.=' AND ( a.username LIKE  "%'.$condition['admin_name'].'%" OR a.nickname LIKE "%'.$condition['admin_name'].'%")';

        }




        if(!empty($condition['start_date']))
        {
            $sql.=" AND ao.create_time >='".$condition['start_date']."'";
        }

        if(!empty($condition['end_date']))
        {
            $sql.="  AND ao.create_time <='".$condition['end_date']."'";
        }

        $res=$this->db->query($sql)->row_array();

        return $res['total'];



    }


}

